<?php

class Admin_CircuitController extends Zend_Controller_Action
{

    public function init()
    {
		if(!Zend_Auth::getInstance()->hasIdentity())  
		{
			$this->_redirect('admin/login');  
		}  
    }

    public function indexAction()
    {
    }
    
    public function addAction()
    {
    	$form = new Application_Form_Circuit();
    	$circuitDAO = new Application_Model_CircuitDAO();
    	$paysDAO = new Application_Model_PaysDAO();
		$this->view->form = $form;
		if($this->getRequest()->isPost())
		{
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData))
			{
				$values = $form->getValues();
				$circuit = new Application_Model_Circuit();
				$circuit->setPays($paysDAO->find($values['paynum']));
				$circuit->setCirNom($values['cirnom']);
				$circuit->setCirLongueur($values['cirlongueur']);
				$circuit->setCirNbSpectateurs($values['cirnbspectateurs']);
				$circuit->setCirAdresseImage($values['ciradresseimage']);
				$circuit->setCirText($values['cirtext']);

				if($circuitDAO->save($circuit))
				{
					$this->_redirect('admin/circuit');
				}
				else
				{
					$form->populate($values);
				}
			}
			else
			{
				$form->populate($form->getValues());
			}
		}
    }
    
    public function updateAction()
    {
    	$circuitDAO = new Application_Model_CircuitDAO();
    	$id = $this->getRequest()->getParam('id');
    	if(!empty($id))
    	{
    		$id = $this->getRequest()->getParam('id');
	        $form = new Application_Form_Circuit();
	    	$paysDAO = new Application_Model_PaysDAO();
	    	$circuit = $circuitDAO->find($id);
	    	if(!is_null($circuit))
	    	{
				$this->view->form = $form;
				$form->populate($circuit->toArray());
				if($this->getRequest()->isPost())
				{
					$formData = $this->getRequest()->getPost();
					if($form->isValid($formData))
					{
						$values = $form->getValues();
						$circuit = new Application_Model_Circuit();
						$circuit->setCirNum($values['cirnum']);
						$circuit->setPays($paysDAO->find($values['paynum']));
						$circuit->setCirNom($values['cirnom']);
						$circuit->setCirLongueur($values['cirlongueur']);
						$circuit->setCirNbSpectateurs($values['cirnbspectateurs']);
						$circuit->setCirAdresseImage($values['ciradresseimage']);
						$circuit->setCirText($values['cirtext']);
		
						if($circuitDAO->save($circuit))
						{
							$this->_redirect('admin/circuit');
						}
						else
						{
							$form->populate($values);
						}
					}
					else
					{
						$form->populate($form->getValues());
					}
				}
			}
			else
			{
				$this->_redirect('admin/circuit');
			}
    	}
    	else
    	{
    		//On liste les grand prix
    		$this->view->circuit = $circuitDAO->findAll();
    	}
    }
	
    public function deleteAction()
    {
        $circuitDAO = new Application_Model_CircuitDAO();
    	$id = $this->getRequest()->getParam('id');
    	if(!empty($id))
    	{
    		$id = $this->getRequest()->getParam('id');
	    	$circuit = $circuitDAO->find($id);
	    	if(!is_null($circuit))
	    	{
				$circuitDAO->delete($circuit->getCirNum());
				$this->_redirect('admin/circuit');
			}
			else
			{
				$this->_redirect('admin/circuit');
			}
    	}
    	else
    	{
    		//On liste les grand prix
    		$this->view->circuit = $circuitDAO->findAll();
    	}
    }
}